//
//  FacePannel.m
//  weibo-01
//
//  Created by HZApple on 16/5/20.
//  Copyright © 2016年 hdu. All rights reserved.
//

#import "FacePannel.h"

@interface FacePannel ()<UIScrollViewDelegate> {
    UIImageView *faceImgView;
}

@end

@implementation FacePannel

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self _crateSubview];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_selectViewChange) name:kFaceViewChange object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_selectViewEnd) name:kFaceViewEnd object:nil];
    }
    return self;
}

- (void)_crateSubview {
    
    _faceView = [[FaceView alloc] initWithFrame:CGRectZero];
    _faceView.clipsToBounds = YES;
    _faceView.backgroundColor = [UIColor clearColor];
    
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, _faceView.height)];
    
    
    
    _scrollView.pagingEnabled = YES;
    
    _scrollView.clipsToBounds = YES;
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(_faceView.width,0);
    [self addSubview:_scrollView];
    [_scrollView addSubview:_faceView];
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, _faceView.bottom, kScreenWidth, 30)];
    _pageControl.numberOfPages = _faceView.width / kScreenWidth;
    _pageControl.currentPage = 0;
    
    
    self.width = kScreenWidth;
    self.height = _faceView.height +_pageControl.height;
    
    [self addSubview:_pageControl];

    
    _selectImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    _selectImgView.image = [UIImage imageNamed:@"emoticon_keyboard_magnifier.png"];
   [_selectImgView sizeToFit];
    _selectImgView.hidden = YES;
   [self addSubview:_selectImgView];
    
    faceImgView = [[UIImageView alloc] initWithFrame:CGRectMake((_selectImgView.width - 30)/2, 15, 30, 30)];
    
    [_selectImgView addSubview:faceImgView];

}

- (void)drawRect:(CGRect)rect {
    UIImage *backGroundImage = [UIImage imageNamed:@"emoticon_keyboard_background"];
    self.backgroundColor = [UIColor colorWithPatternImage:backGroundImage];
    
     //faceImgView.image = _faceView.faceInfo[@"png"];
}
- (void)_selectViewChange {
    _selectImgView.center = _faceView.viewCenter;
    _selectImgView.bottom = _faceView.viewBottom;
    _selectImgView.hidden = _faceView.viewHidden;
    faceImgView.image = [UIImage imageNamed:_faceView.faceInfo[@"png"]];
}

- (void)_selectViewEnd {
    _selectImgView.hidden = YES;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat x = scrollView.contentOffset.x;
    _pageControl.currentPage = x / kScreenWidth;
}

@end
